package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Setting;

public class SettingDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Setting setting) {
		open();
		this.sql = "INSERT INTO Settings (prince_fine_setting, amount_day_loan_setting, institution_name_setting) VALUES (?, ?, ?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setDouble(1, setting.getPrinceFineSetting());
			this.statement.setInt(2, setting.getAmountDayLoanSetting());
			this.statement.setString(3, setting.getInstitutionNameSetting());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Setting setting) {
		open();
		this.sql = "UPDATE Settings SET prince_fine_setting = ?, amount_day_loan_setting = ?, institution_name_setting = ?  WHERE id_setting = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setDouble(1, setting.getPrinceFineSetting());
			this.statement.setInt(2, setting.getAmountDayLoanSetting());
			this.statement.setString(3, setting.getInstitutionNameSetting());
			this.statement.setInt(4, setting.getIdSetting());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Setting setting) {
		open();
		this.sql = "DELETE FROM Settings WHERE id_setting = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, setting.getIdSetting());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Setting search(Integer id_setting) {
		open();
		this.sql = "SELECT * FROM Settings WHERE id_setting = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_setting);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Setting(result.getInt("id_setting"),
						result.getDouble("prince_fine_setting"),
						result.getInt("amount_day_loan_setting"),
						result.getString("institution_name_setting"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Setting> list() {
		open();
		this.sql = "SELECT * FROM Settings";
		ArrayList<Setting> settings = new ArrayList<Setting>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				settings.add(new Setting(result.getInt("id_setting"), result
						.getDouble("prince_fine_setting"), result
						.getInt("amount_day_loan_setting"), result
						.getString("institution_name_setting")));
			}
			return settings;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
